package com.jstyle.jclifexd.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.daoManager.NotifyDataDaoManager;
import com.jstyle.jclifexd.model.NotifyData;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.utils.Utils;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/14.
 */

public class NotifyDataAdapter extends RecyclerViewBaseAdapter {

    final int VIEWTYPE_NORMAL = 0;
    final int VIEWTYPE_OTHER = 1;
    public NotifyDataAdapter(List<NotifyData> notifyDataList) {
        super(notifyDataList);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new NormalViewHolder(view);
                break;
            case VIEWTYPE_OTHER:
                View viewOther = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new OtherViewHolder(viewOther);
                break;
        }
        return baseViewHolder;
    }

    public class OtherViewHolder extends BaseViewHolder {

        public OtherViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public class NormalViewHolder extends BaseViewHolder {

        @BindView(R.id.button_notify_name)
        TextView buttonNotifyName;
        @BindView(R.id.switch1)
        SwitchCompat switch1;
        @BindView(R.id.textView_notifytips)
        TextView textViewNotifyTips;
        @BindArray(R.array.notify_array)
        String[]notifyArray;
        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case VIEWTYPE_NORMAL:
                bindNormal(holder, position);
                break;
        }


    }

    @SuppressLint("SetTextI18n")
    private void bindNormal(BaseViewHolder holder, int position) {
        NormalViewHolder normalViewHolder=(NormalViewHolder)holder;
       // normalViewHolder.textViewNotifyTips.setVisibility(position==0?View.INVISIBLE:View.VISIBLE);
        final NotifyData notifyData= (NotifyData) mDataList.get(position);
        String name=normalViewHolder.notifyArray[position];
        normalViewHolder.buttonNotifyName.setText(name);
        Context context=normalViewHolder.itemView.getContext();
        if(!Utils.IsRussian(context)) {
        normalViewHolder.textViewNotifyTips.setText(name+" "+context.getString(R.string.show_screen));
        }else{
            normalViewHolder.textViewNotifyTips.setText(context.getString(R.string.show_screen));
            String arrvat[]=context.getResources().getStringArray(R.array.notify_valiues);
            normalViewHolder.textViewNotifyTips.setText(arrvat[position]);
        }
        normalViewHolder.switch1.setChecked(notifyData.getEnable());
        normalViewHolder.switch1.setClickable(getAllEnable());

        normalViewHolder.switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                notifyData.setEnable(isChecked);
                NotifyDataDaoManager.updateNotifyData(notifyData);
            }
        });
    }

    private boolean getAllEnable(){
        return  NotifyDataDaoManager.getNotifyData(99).getEnable();
    }
    public void updateDate(List<NotifyData>notifyDataList){
        this.mDataList=notifyDataList;
        notifyDataSetChanged();
    }
    @Override
    protected int getLayoutId(int viewType) {
        int id = R.layout.item_notifydata;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                id = R.layout.item_notifydata;
                break;
            case VIEWTYPE_OTHER:
                id = R.layout.item_other_nofity;
                break;

        }
        return id;
    }
    @Override
    public int getItemViewType(int position) {
        int type = VIEWTYPE_NORMAL;

        return type;
    }
}
